package com.vogue.supplymall.order.task;

import com.vogue.ssm.database.util.DealStatus;
import com.vogue.supplymall.order.domain.biz.TimeoutTaskProcessorBiz;
import com.vogue.supplymall.order.domain.service.CommentDetailService;
import com.vogue.supplymall.order.domain.service.OrderStatusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * The type Timed task utils.
 * 订单模块的定时任务
 */
@Component("TaskOrderCancel")
public class TimedTaskUtils {

    /**
     * The constant LOGGER_TIMEDTASKUTILS.
     */
    public static final Logger LOGGER_TIMEDTASKUTILS = LoggerFactory.getLogger(TimedTaskUtils.class);

    @Resource
    private OrderStatusService orderStatusService;
    @Resource
    private TimeoutTaskProcessorBiz timeoutTaskProcessorBiz;
    @Resource
    private CommentDetailService commentDetailService;

    /**
     * 自动更新自动取消的订单的标志
     */
    @Scheduled(cron = "1 0 0 * * ?")
    public void updateOrderCancelState() {
        LOGGER_TIMEDTASKUTILS.info("update order cancel state begin time :"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            DealStatus.kacha();
            orderStatusService.updateCancelState();
        } catch (Exception ex) {
            LOGGER_TIMEDTASKUTILS.error(">>>SOS<<< update order cancel state err :" + ex);
        } finally {
            LOGGER_TIMEDTASKUTILS.info("update order cancel state end time :"
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
    }

    /**
     * Update order timeout receiving.
     * 30天自动确认收货，确认收货时有差异自动添加退单并退款
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateOrderTimeoutReceiving() {
        LOGGER_TIMEDTASKUTILS.info("update order auto complete begin time :"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            DealStatus.kacha();
            timeoutTaskProcessorBiz.updateTimeoutReceiving();
        } catch (Exception ex) {
            LOGGER_TIMEDTASKUTILS.error(">>>SOS<<< update order timeout receiving state err :" + ex);
        } finally {
            LOGGER_TIMEDTASKUTILS.info("update order timeout receiving state end time :"
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
    }

    /**
     * 自动更新买家好评 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
     */
    @Scheduled(cron = "0 0 2 * * ?")//每天早上2点执行一次
    public void updateCommentText() {
        LOGGER_TIMEDTASKUTILS.info("update comment_detail commenttext state begin time :"
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            DealStatus.kacha();
            commentDetailService.updateCommentText();
        } catch (Exception ex) {
            LOGGER_TIMEDTASKUTILS.error(">>>SOS<<< update comment_detail commenttext state err :" + ex);
        } finally {
            LOGGER_TIMEDTASKUTILS.info("update comment_detail commenttext state end time :"
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
    }
}
